﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp4
{
    class DbHelper
    {
        private static readonly string conString = "server=.;database=Blogs;uid=hzh;pwd=hzh2000623;";

        /// <summary>
        /// 获得表数据
        /// </summary>
        /// <param name="sql">查询的select语句</param>
        /// <returns>表数据</returns>
        public static DataTable GetDataTable(string sql)
        {
            SqlConnection sqlConnection = new SqlConnection(conString);
            sqlConnection.Open();

            //string cmdsString = "select * from students";

            SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, sqlConnection);

            DataTable dt = new DataTable();

            dataAdapter.Fill(dt);

            return dt;
        }


        /// <summary>
        /// 增加或修改的方法
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns> 
        public static int AddOrEditOrDeleteSave(string sql)
        {
            SqlConnection sqlConnection = new SqlConnection(conString);  //初始化连接对象

            SqlCommand command = new SqlCommand(sql, sqlConnection);   //初始化命令对象

            sqlConnection.Open();

            return command.ExecuteNonQuery();

        }
    }
}
